********************************************************************************************************************
*****Do-file for Table A17
*****Who is mobilized to vote by short text messages? Evidence from a nationwide field experiment with young voters
********************************************************************************************************************
*****Last edited 24/6/5
********************************************************************************************************************
*****Ado packages needed: estout
********************************************************************************************************************


clear all

*Programs to calculate group differences with standard errors and stars
capture program drop, myrepost
program myrepost, eclass
ereturn repost b=`1'
ereturn repost V=`2'
ereturn scalar df_r=`3'
end

capture program drop mystars
program mystars, eclass
local d_stars=string(`1', "%9.3f")
local pval=ttail(`3',abs(`1'/`2'))*2

if `pval'<=0.01 {
local d_stars="`d_stars'"+"***"		
}

if `pval'>0.01 & `pval'<=0.05  {
local d_stars="`d_stars'"+"**"		
}

if `pval'>0.05 & `pval'<=0.1  {
local d_stars="`d_stars'"+"*"		
}

local se_stars=string(`2', "%9.3f")

local se_stars="("+"`se_stars'"+")"

ereturn local d_stars="`d_stars'"
ereturn local se_stars="`se_stars'"


end

*Use data
use \data\dataforanalysis230522_v2.dta, clear
*Set basecategory
fvset base 8 moses1d


*************************************************
*****TABLE A17
*************************************************


label variable treated "Treated"

*Generate voting propensity groups for direct effects
sum pvote_mother if voted22!=.  & treated!=. & female!=., detail
gen marginal=.
replace marginal=1 if pvote_mother>=r(p25) & pvote_mother<r(p75)
replace marginal=0 if pvote_mother<r(p25) | (pvote_mother>=r(p75) & pvote_mother!=.)

gen never=.
replace never=1 if pvote_mother<r(p25)
replace never=0 if pvote_mother>=r(p25) & pvote_mother!=.

gen always=.
replace always=1 if pvote_mother>=r(p75) & pvote_mother!=.
replace always=0 if pvote_mother<r(p75)



eststo clear

eststo all: reg voted22 treated molincome female age i.moses1d firstvote i.mohighschool foreign if marginal!=. & voted21==1, cluster(kunta19)

estadd local controls "Yes"
estimates store all

qui: mean voted22 if treated==0 & e(sample)==1
estadd scalar umean=r(table)[1,1]: all

qui: reg voted22 treated molincome female age i.moses1d firstvote i.mohighschool foreign if marginal==1 & voted21==1, cluster(kunta19)

matrix Brep1=e(b)
matrix Vrep1=e(V)
scalar df1=e(df_r)
qui: reg voted22 treated molincome female age i.moses1d firstvote i.mohighschool foreign if never==1 & voted21==1, cluster(kunta19)

matrix Brep2=e(b)
matrix Vrep2=e(V)
scalar df2=e(df_r)

scalar df=df1+df2

scalar diffe=Brep1[1,1]-Brep2[1,1]
scalar stde=sqrt(Vrep1[1,1]+Vrep2[1,1])

eststo low: reg voted22 treated molincome female age i.moses1d firstvote i.mohighschool foreign if never==1 & voted21==1, cluster(kunta19)
mystars diffe stde df
estadd local label1 "Marginal"
estadd local label2 "- Low"
estadd local controls "Yes"
estimates store low
qui: mean voted22 if treated==0 & e(sample)==1
estadd scalar umean=r(table)[1,1]: low

qui: reg voted22 treated molincome female age i.moses1d firstvote i.mohighschool foreign if marginal==1 & voted21==1, cluster(kunta19)
matrix Brep1=e(b)
matrix Vrep1=e(V)
scalar df1=e(df_r)


qui: reg voted22 treated molincome female age i.moses1d firstvote i.mohighschool foreign if always==1 & voted21==1, cluster(kunta19)
matrix Brep2=e(b)
matrix Vrep2=e(V)
scalar df2=e(df_r)

scalar df=df1+df2

scalar diffe=Brep1[1,1]-Brep2[1,1]
scalar stde=sqrt(Vrep1[1,1]+Vrep2[1,1])

eststo marginal: reg voted22 treated molincome female age i.moses1d firstvote i.mohighschool foreign if marginal==1 & voted21==1, cluster(kunta19)

mystars diffe stde df
estadd local label1 "Marginal"
estadd local label2 "- High"
estadd local controls "Yes"
estimates store marginal
qui: mean voted22 if treated==0 & e(sample)==1
estadd scalar umean=r(table)[1,1]: marginal


qui: reg voted22 treated molincome female age i.moses1d firstvote i.mohighschool foreign if always==1 & voted21==1, cluster(kunta19)
matrix Brep1=e(b)
matrix Vrep1=e(V)
scalar df1=e(df_r)


qui: reg voted22 treated molincome female age i.moses1d firstvote i.mohighschool foreign if never==1 & voted21==1, cluster(kunta19)
matrix Brep2=e(b)
matrix Vrep2=e(V)
scalar df2=e(df_r)

scalar df=df1+df2

scalar diffe=Brep1[1,1]-Brep2[1,1]
scalar stde=sqrt(Vrep1[1,1]+Vrep2[1,1])

eststo high: reg voted22 treated molincome female age i.moses1d firstvote i.mohighschool foreign if always==1 & voted21==1, cluster(kunta19)

mystars diffe stde df
estadd local label1 "High"
estadd local label2 "- Low"
estadd local controls "Yes"
estimates store high
qui: mean voted22 if treated==0 & e(sample)==1
estadd scalar umean=r(table)[1,1]: high

local header "& \multicolumn{4}{c}{Outcome: Voted} \\"
local titles1 `"& All & "Low Propensity" & "Marginal Voters" & "High Propensity"\\ "'
local titles2 "& & Bottom 25\% & 25-75\% & Top 25\% \\"
local numbers "& (1) & (2) & (3) & (4) \\ \hline"
local emptyrow "& & & &  \\ "
local line "& & & & \hline \\ "

esttab using byvprop_voted21.tex, keep (treated) noobs label se b(%9.3f) se(%9.3f) star(* 0.10 ** 0.05 *** 0.01) ///
scalars("controls Controls " "umean Untreated $\bar{Y}$" "N Observations" "label1 \hline" "label2 \phantom{label2}" "d_stars Differences" "se_stars \phantom{se}") ///
sfmt(%9.3f %9.3f %9.0fc %9.3f) mlabels(none) nonumbers prehead(\begin{table}[htbp]\centering ///
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} ///
\caption{Heterogeneity by Vote Propensity} ///
\begin{tabular}{l*{4}{c}}\hline\hline) posthead("`header'" "`emptyrow'" `"`titles1'"' "`titles2'" "`numbers'" \\ \multicolumn{4}{c}{\textbf{Panel A: Voted in 2021}} \\ "`emptyrow'")  ///
refcat(treated "", nolabel below) postfoot(\hline) fragment nonotes replace


eststo clear


eststo all: reg voted22 treated molincome female age i.moses1d firstvote i.mohighschool foreign if marginal!=. & voted21==0, cluster(kunta19)

estadd local controls "Yes"
estimates store all

qui: mean voted22 if treated==0 & e(sample)==1
estadd scalar umean=r(table)[1,1]: all

qui: reg voted22 treated molincome female age i.moses1d firstvote i.mohighschool foreign if marginal==1 & voted21==0, cluster(kunta19)

matrix Brep1=e(b)
matrix Vrep1=e(V)
scalar df1=e(df_r)
qui: reg voted22 treated molincome female age i.moses1d firstvote i.mohighschool foreign if never==1 & voted21==0, cluster(kunta19)

matrix Brep2=e(b)
matrix Vrep2=e(V)
scalar df2=e(df_r)

scalar df=df1+df2

scalar diffe=Brep1[1,1]-Brep2[1,1]
scalar stde=sqrt(Vrep1[1,1]+Vrep2[1,1])

eststo low: reg voted22 treated molincome female age i.moses1d firstvote i.mohighschool foreign if never==1 & voted21==0, cluster(kunta19)
mystars diffe stde df
estadd local label1 "Marginal"
estadd local label2 "- Low"
estadd local controls "Yes"
estimates store low
qui: mean voted22 if treated==0 & e(sample)==1
estadd scalar umean=r(table)[1,1]: low

qui: reg voted22 treated molincome female age i.moses1d firstvote i.mohighschool foreign if marginal==1 & voted21==0, cluster(kunta19)
matrix Brep1=e(b)
matrix Vrep1=e(V)
scalar df1=e(df_r)


qui: reg voted22 treated molincome female age i.moses1d firstvote i.mohighschool foreign if always==1 & voted21==0, cluster(kunta19)
matrix Brep2=e(b)
matrix Vrep2=e(V)
scalar df2=e(df_r)

scalar df=df1+df2

scalar diffe=Brep1[1,1]-Brep2[1,1]
scalar stde=sqrt(Vrep1[1,1]+Vrep2[1,1])

eststo marginal: reg voted22 treated molincome female age i.moses1d firstvote i.mohighschool foreign if marginal==1 & voted21==0, cluster(kunta19)

mystars diffe stde df
estadd local label1 "Marginal"
estadd local label2 "- High"
estadd local controls "Yes"
estimates store marginal
qui: mean voted22 if treated==0 & e(sample)==1
estadd scalar umean=r(table)[1,1]: marginal


qui: reg voted22 treated molincome female age i.moses1d firstvote i.mohighschool foreign if always==1 & voted21==0, cluster(kunta19)
matrix Brep1=e(b)
matrix Vrep1=e(V)
scalar df1=e(df_r)


qui: reg voted22 treated molincome female age i.moses1d firstvote i.mohighschool foreign if never==1 & voted21==0, cluster(kunta19)
matrix Brep2=e(b)
matrix Vrep2=e(V)
scalar df2=e(df_r)

scalar df=df1+df2

scalar diffe=Brep1[1,1]-Brep2[1,1]
scalar stde=sqrt(Vrep1[1,1]+Vrep2[1,1])

eststo high: reg voted22 treated molincome female age i.moses1d firstvote i.mohighschool foreign if always==1 & voted21==0, cluster(kunta19)

mystars diffe stde df
estadd local label1 "High"
estadd local label2 "- Low"
estadd local controls "Yes"
estimates store high
qui: mean voted22 if treated==0 & e(sample)==1
estadd scalar umean=r(table)[1,1]: high








esttab, keep (treated) noobs label se b(%9.3f) se(%9.3f) star(* 0.10 ** 0.05 *** 0.01) ///
scalars("controls Controls " "umean Untreated $\bar{Y}$" "N Observations" "label1 \hline" "label2 \phantom{label2}" "d_stars Differences" "se_stars \phantom{se}") ///
sfmt(%9.3f %9.3f %9.0fc %9.3f) mlabels(none) nonumbers prefoot(\hline)  posthead("`emptyrow'" \multicolumn{4}{c}{\textbf{Panel B: Did Not Vote in 2021}} \\ "`emptyrow'")  ///
refcat(treated "", nolabel below) postfoot(\hline\hline \\ \end{tabular} \\ \end{table}) fragment append nonotes 